package com.ml.data;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.virgil.redis.util.RedisUtil;

@Component
public class GenerateData {

    public final static Logger LOGGER = LoggerFactory.getLogger(GenerateData.class);

    @Autowired
    RedisUtil redisUtil;

    @Async
    @Scheduled(cron = "0 0/30 * * * ?")
    public void coinToCny() {
        RestTemplate restTemplate = new RestTemplate();
        try {
            ResponseEntity<String> result = restTemplate.getForEntity("https://otc-api.huobi.co/v1/data/market/detail", String.class);
            JSONObject o = JSON.parseObject(result.getBody());

            if(!"true".equals(o.getString("success"))) {
                LOGGER.info("huobi---获取cny汇率失败");
                return;
            }
            JSONArray array = JSON.parseObject(o.getString("data")).getJSONArray("detail");
            redisUtil.set("api:huobi:coinToCny",array.toJSONString() );
            LOGGER.info("huobi---最新cny汇率:"+array);
        } catch (Exception e) {
            LOGGER.error("huobi---获取cny汇率失败");
        }
    }

   
}
